<?php

namespace App\Exports;

use App\Models\RedForm;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;

class RedFormExport implements FromCollection,WithHeadings
{
    private $RedForm = null;
    private $_red_id = 0;
    /**
     * RedFormExport constructor.
     * @param $_red_id
     */
    public function __construct($_red_id)
    {
        $this->_red_id = $_red_id;
        $this->RedForm = RedForm::where("red_id",$_red_id)
            ->select("form_content","created_at")
            ->orderBy("id","asc")
            ->get();
    }

    /**
    * @return \Illuminate\Support\Collection
    */
    public function collection()
    {
        $_data = array();
        foreach($this->RedForm as $_k=>$item) {
            $_data[$_k] = array();
            foreach($item->form_content as $value) {
                $_data[$_k][] = $value["value"];
            }
            $_data[$_k][] = date('Y-m-d H:i:s',strtotime($item->created_at));
        }
        return new Collection($_data);
    }

    /**
     * @inheritDoc
     */
    public function headings(): array
    {
        $_form_content = RedForm::where("red_id",$this->_red_id)
            ->value("form_content");
        $_heading = array();
        foreach ($_form_content as $_value) {
            $_heading[] = $_value['label'];
        }
        $_heading[] = '时间';
        return $_heading;
    }
}
